﻿// admin1.cpp: 实现文件
//

#include "pch.h"
#include "luntan.h"
#include "admin1.h"
#include "afxdialogex.h"
#include"admin.h"


// admin1 对话框

IMPLEMENT_DYNAMIC(admin1, CDialogEx)

admin1::admin1(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_ADMIN1, pParent)
	, m_nick(_T(""))
	, m_uuserpwd(_T(""))
	, m_score(_T(""))
	, m_susername(_T(""))
	, m_susernick(_T(""))
{

}

admin1::~admin1()
{
}

void admin1::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_list2);
	DDX_Text(pDX, IDC_EDIT1, m_nick);
	DDX_Text(pDX, IDC_EDIT2, m_uuserpwd);
	DDX_Text(pDX, IDC_EDIT7, m_score);
	DDX_Text(pDX, IDC_EDIT3, m_susername);
	DDX_Text(pDX, IDC_EDIT4, m_susernick);
}


BEGIN_MESSAGE_MAP(admin1, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON8, &admin1::OnBnClickedButton8)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, &admin1::OnNMClickList1)
	ON_BN_CLICKED(IDC_BUTTON6, &admin1::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &admin1::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON1, &admin1::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON3, &admin1::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON9, &admin1::OnBnClickedButton9)
END_MESSAGE_MAP()


// admin1 消息处理程序


void admin1::OnBnClickedButton8()
{
	// TODO: 在此添加控件通知处理程序代码
	admin1::OnOK();
	admin a;
	a.DoModal();

}


BOOL admin1::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	m_list2.SetExtendedStyle(LVS_EX_FLATSB //扁平风格显示滚动条
		| LVS_EX_FULLROWSELECT //允许整行选中
		| LVS_EX_HEADERDRAGDROP //允许整列拖动
		| LVS_EX_ONECLICKACTIVATE //单击选中项
		| LVS_EX_GRIDLINES); //画出网格线

	//设置表头
	m_list2.InsertColumn(0, _T("用户名"), LVCFMT_LEFT, 100, 25);
	m_list2.InsertColumn(1, _T("用户密码"), LVCFMT_LEFT, 120, 25);
	m_list2.InsertColumn(2, _T("用户昵称"), LVCFMT_LEFT, 100, 25);
	m_list2.InsertColumn(3, _T("性别"), LVCFMT_LEFT, 60, 25);
	m_list2.InsertColumn(4, _T("用户积分"), LVCFMT_LEFT, 100, 25);
	m_list2.InsertColumn(5, _T("用户等级"), LVCFMT_LEFT, 100, 25);
	m_list2.InsertColumn(6, _T("注册时间"), LVCFMT_LEFT, 150, 25);


	AddToGrid();

	// TODO:  在此添加额外的初始化

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}



void admin1::AddToGrid()
{
	// TODO: 在此处添加实现代码.
	ADOConn m_AdoConn; //ADOConn对象
	m_AdoConn.OnInitADOConn(); //连接数据库
	CString sql;
	int i = 0;
	sql.Format("select User_Name,User_Pwd,User_nickname,User_sex,User_score,Grade.Grade_name,User_createtime from Users, Grade\
	where Users.Grade_id = Grade.Grade_id");
	m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql); //根据SQL语句进行查询
	//MessageBox((_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Name"));
	while (!m_AdoConn.m_pRecordset->adoEOF)
	{
		m_list2.InsertItem(i, ""); //向列表视图控件中插入行
		//向列表视图控件中插入列
		m_list2.SetItemText(i, 0, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Name"));
		m_list2.SetItemText(i, 1, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Pwd"));
		m_list2.SetItemText(i, 2, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_nickname"));
		m_list2.SetItemText(i, 3, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_sex"));
		m_list2.SetItemText(i, 4, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_score"));
		m_list2.SetItemText(i, 5, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Grade_name"));
		m_list2.SetItemText(i, 6, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_createtime"));
		m_AdoConn.m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录
		i++;
	}
	m_AdoConn.ExitConnect();
}


void admin1::OnNMClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	int pos = m_list2.GetSelectionMark();
	username2= m_list2.GetItemText(pos, 0);
	*pResult = 0;
}


void admin1::OnBnClickedButton6()
{
	// TODO: 在此添加控件通知处理程序代码
	if (username2 != "")
	{
		ADOConn m_AdoConn; //ADOConn对象
		m_AdoConn.OnInitADOConn(); //连接数据库
		CString sql;
		sql.Format("delete from Users where User_Name='%s'", username2);
		m_AdoConn.ExecuteSQL((_bstr_t)sql);
		m_AdoConn.ExitConnect();
		m_list2.DeleteAllItems();
		AddToGrid(); //向列表视图控件中插入数据
		username2 = "";
	}
	else
	{
		MessageBox("请选择要删除的用户！", "提示", MB_ICONWARNING);
	}
	
}


void admin1::OnBnClickedButton7()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_nick != "" && m_uuserpwd != "" && m_score != "")
	{
		
		if (username2 != "")
		{
			ADOConn m_AdoConn; //ADOConn对象
			m_AdoConn.OnInitADOConn(); //连接数据库
			CString sql;
			sql.Format("update Users set User_nickname='%s',User_Pwd='%s',User_score='%s' where User_Name='%s'", m_nick, m_uuserpwd, m_score, username2);
			m_AdoConn.ExecuteSQL((_bstr_t)sql);
			m_AdoConn.ExitConnect();
			m_nick = "", m_uuserpwd = "", m_score = "";
			UpdateData(FALSE);
			m_list2.DeleteAllItems();
			AddToGrid(); //向列表视图控件中插入数据
			username2 = "";
		}
		else
		{
			MessageBox("请选择所要更改的用户！", "提示", MB_ICONWARNING);
			
		}
		
	}
	else
	{
		MessageBox("修改信息不能为空！", "提示", MB_ICONWARNING);
	}
	
}


void admin1::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	m_list2.DeleteAllItems();
	AddToGrid(); //向列表视图控件中插入数据
}


void admin1::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_susername != "")
	{
		m_list2.DeleteAllItems();
		ADOConn m_AdoConn;
		m_AdoConn.OnInitADOConn();
		CString sql;
		int i = 0;
		sql.Format("select User_Name,User_Pwd,User_nickname,User_sex,User_score,Grade.Grade_name,User_createtime\
	                from Users, Grade where Users.Grade_id = Grade.Grade_id and User_Name like '%s%s%s'", \
			"%", m_susername, "%"); //设置SQL语句
		m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql); //根据SQL语句进行查询
		while (!m_AdoConn.m_pRecordset->adoEOF)
		{
			m_list2.InsertItem(i, ""); //向列表视图控件中插入行
			//向列表视图控件中插入列
			m_list2.SetItemText(i, 0, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Name"));
			m_list2.SetItemText(i, 1, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Pwd"));
			m_list2.SetItemText(i, 2, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_nickname"));
			m_list2.SetItemText(i, 3, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_sex"));
			m_list2.SetItemText(i, 4, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_score"));
			m_list2.SetItemText(i, 5, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Grade_name"));
			m_list2.SetItemText(i, 6, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_createtime"));
			m_AdoConn.m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录
			i++;
		}
		m_AdoConn.ExitConnect();
		m_susername = "";
		UpdateData(false);
	}
	else
	{
		MessageBox("查询条件不能为空！", "提示", MB_ICONWARNING);
	}
}


void admin1::OnBnClickedButton9()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_susernick != "")
	{
		m_list2.DeleteAllItems();
		ADOConn m_AdoConn;
		m_AdoConn.OnInitADOConn();
		CString sql;
		int i = 0;
		sql.Format("select User_Name,User_Pwd,User_nickname,User_sex,User_score,Grade.Grade_name,User_createtime\
	                from Users, Grade where Users.Grade_id = Grade.Grade_id and User_nickname like '%s%s%s'", \
			"%", m_susernick, "%"); //设置SQL语句
		m_AdoConn.m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql); //根据SQL语句进行查询
		while (!m_AdoConn.m_pRecordset->adoEOF)
		{
			m_list2.InsertItem(i, ""); //向列表视图控件中插入行
			//向列表视图控件中插入列
			m_list2.SetItemText(i, 0, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Name"));
			m_list2.SetItemText(i, 1, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_Pwd"));
			m_list2.SetItemText(i, 2, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_nickname"));
			m_list2.SetItemText(i, 3, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_sex"));
			m_list2.SetItemText(i, 4, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_score"));
			m_list2.SetItemText(i, 5, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("Grade_name"));
			m_list2.SetItemText(i, 6, (_bstr_t)m_AdoConn.m_pRecordset->GetCollect("User_createtime"));
			m_AdoConn.m_pRecordset->MoveNext(); //将记录集指针移动到下一条记录
			i++;
		}
		m_AdoConn.ExitConnect();
		m_susernick = "";
		UpdateData(false);
	}
	else
	{
		MessageBox("查询条件不能为空！", "提示", MB_ICONWARNING);
	}
}
